#include "utils.h"
#include "qcoreapplication.h"

#include <Config.h>
#include <QCheckBox>
#include <QEventLoop>
#include <QJsonDocument>
#include <QLabel>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QProgressDialog>
#include <QStandardPaths>

Utils::Utils() : QMainWindow()
{
    backShowStyleKeys << "默认(default)" << "主要(primary)" << "成功(success)"
                      << "信息(info)" << "警告(warning)" << "危险(danger)";
    backShowStyle.insert("default","");
    backShowStyle.insert("primary","color:rgb(24,144,255); background-color:rgb(234,244,255);border:1px solid rgb(209,233,255);border-radius:5px;");
    backShowStyle.insert("success","color:rgb(19,206,102); background-color:rgb(231,250,240);border:1px solid rgb(208,245,224);border-radius:5px;");
    backShowStyle.insert("info",   "color:rgb(144,147,153);background-color:rgb(244,244,245);border:1px solid rgb(233,233,235);border-radius:5px;");
    backShowStyle.insert("warning","color:rgb(255,186,0);  background-color:rgb(255,248,230);border:1px solid rgb(255,241,204);border-radius:5px;");
    backShowStyle.insert("danger", "color:rgb(255,73,73);  background-color:rgb(255,237,237);border:1px solid rgb(255,219,219);border-radius:5px;");
}

User* Utils::getUser(QByteArray bytes)
{
    User* user = new User;
    QJsonObject json = QJsonDocument::fromJson(bytes).object();
    if(json.value("code").toInt() != 200){
        QMessageBox::information(0,"提示",json.value("msg").toString());
        return user;
    }else{
        QJsonObject obj = json.value("user").toObject();
        if(obj.isEmpty()){
            obj = json.value("data").toObject();
        }
        user->createBy = obj.value("createBy").toString();
        user->createTime = obj.value("createTime").toString();
        user->updateBy = obj.value("updateBy").toString();
        user->updateTime = obj.value("updateTime").toString();
        user->remark = obj.value("remark").toString();
        user->userId = obj.value("userId").toInt();
        user->deptId = obj.value("deptId").toInt();
        user->userName = obj.value("userName").toString();
        user->nickName = obj.value("nickName").toString();
        user->email = obj.value("email").toString();
        user->phonenumber = obj.value("phonenumber").toString();
        user->sex = obj.value("sex").toString();
        user->avatar = obj.value("avatar").toString();
        user->password = obj.value("password").toString();
        user->status = obj.value("status").toString();
        user->delFlag = obj.value("delFlag").toInt();
        user->loginIp = obj.value("loginIp").toString();
        user->loginDate = obj.value("loginDate").toString();

        QJsonArray jsonRoleIds = obj.value("roleIds").toArray();
        QList<int> roleIds;
        for(int i = 0;i< jsonRoleIds.size();i++){
            roleIds.append(jsonRoleIds.at(i).toInt());
        }
        user->roleIds = roleIds;

        QJsonArray jsonPostIdsIds = obj.value("roleIds").toArray();
        QList<int> postIds;
        for(int i = 0;i< jsonPostIdsIds.size();i++){
            postIds.append(jsonPostIdsIds.at(i).toInt());
        }
        user->postIds = postIds;
        user->roleId = obj.value("roleId").toInt();
        user->admin = obj.value("admin").toInt();

        Dept dept = getDept(obj.value("dept").toObject());
        user->dept = dept;

        QJsonArray roleArr = obj.value("roles").toArray();
        QList<Role> roles;
        for(int i = 0;i< roleArr.size();i++){
            Role role = getRole(roleArr.at(i).toObject());
            roles.append(role);
        }
        user->roles = roles;

        return user;
    }
}

User *Utils::getUserByJsonObj(QJsonObject obj)
{
    User *user = new User;
    user->createBy = obj.value("createBy").toString();
    user->createTime = obj.value("createTime").toString();
    user->updateBy = obj.value("updateBy").toString();
    user->updateTime = obj.value("updateTime").toString();
    user->remark = obj.value("remark").toString();
    user->userId = obj.value("userId").toInt();
    user->deptId = obj.value("deptId").toInt();
    user->userName = obj.value("userName").toString();
    user->nickName = obj.value("nickName").toString();
    user->email = obj.value("email").toString();
    user->phonenumber = obj.value("phonenumber").toString();
    user->sex = obj.value("sex").toString();
    user->avatar = obj.value("avatar").toString();
    user->password = obj.value("password").toString();
    user->status = obj.value("status").toString();
    user->delFlag = obj.value("delFlag").toInt();
    user->loginIp = obj.value("loginIp").toString();
    user->loginDate = obj.value("loginDate").toString();

    QJsonArray jsonRoleIds = obj.value("roleIds").toArray();
    QList<int> roleIds;
    for(int i = 0;i< jsonRoleIds.size();i++){
        roleIds.append(jsonRoleIds.at(i).toInt());
    }
    user->roleIds = roleIds;

    QJsonArray jsonPostIdsIds = obj.value("roleIds").toArray();
    QList<int> postIds;
    for(int i = 0;i< jsonPostIdsIds.size();i++){
        postIds.append(jsonPostIdsIds.at(i).toInt());
    }
    user->postIds = postIds;
    user->roleId = obj.value("roleId").toInt();
    user->admin = obj.value("admin").toInt();

    Dept dept = getDept(obj.value("dept").toObject());
    user->dept = dept;

    QJsonArray roleArr = obj.value("roles").toArray();
    QList<Role> roles;
    for(int i = 0;i< roleArr.size();i++){
        Role role = getRole(roleArr.at(i).toObject());
        roles.append(role);
    }
    user->roles = roles;

    return user;
}

Dept Utils::getDept(QJsonObject jsonDept)
{
    Dept dept;
    dept.createBy = jsonDept.value("createBy").toString();
    dept.createTime = jsonDept.value("createTime").toString();
    dept.updateBy = jsonDept.value("updateBy").toString();
    dept.updateTime = jsonDept.value("updateTime").toString();
    dept.remark = jsonDept.value("remark").toString();
    dept.deptId = jsonDept.value("deptId").toInt();
    dept.parentId = jsonDept.value("parentId").toInt();
    dept.ancestors = jsonDept.value("ancestors").toString();
    dept.deptName = jsonDept.value("deptName").toString();
    dept.orderNum = jsonDept.value("orderNum").toInt();
    dept.leader = jsonDept.value("leader").toString();
    dept.phone = jsonDept.value("phone").toString();
    dept.email = jsonDept.value("email").toString();
    dept.status = jsonDept.value("status").toString();
    dept.delFlag = jsonDept.value("delFlag").toInt();
    dept.parentName = jsonDept.value("parentName").toString();
    QJsonArray arrChildren = jsonDept.value("children").toArray();
    QList<Dept> children;
    for(int i = 0;i< arrChildren.size();i++){
        Dept dept = getDept(arrChildren[i].toObject());
        children.append(dept);
    }
    dept.children = children;
    return dept;
}

Role Utils::getRole(QJsonObject jsonRole)
{
    Role role;
    role.createBy = jsonRole.value("createBy").toString();
    role.createTime = jsonRole.value("createTime").toString();
    role.updateBy = jsonRole.value("updateBy").toString();
    role.updateTime = jsonRole.value("updateTime").toString();
    role.remark = jsonRole.value("remark").toString();
    role.roleId = jsonRole.value("roleId").toInt();
    role.roleName = jsonRole.value("roleName").toString();
    role.roleKey = jsonRole.value("roleKey").toString();
    role.roleSort = jsonRole.value("roleSort").toInt();
    role.dataScope = jsonRole.value("dataScope").toString();
    role.menuCheckStrictly = jsonRole.value("menuCheckStrictly").toInt();
    role.deptCheckStrictly = jsonRole.value("deptCheckStrictly").toInt();
    role.status = jsonRole.value("status").toString();
    role.delFlag = jsonRole.value("delFlag").toInt();
    role.flag = jsonRole.value("flag").toInt();

    QJsonArray jsonMenuIds = jsonRole.value("menuIds").toArray();
    QList<int> menuIds;
    for(int i = 0;i< jsonMenuIds.size();i++){
        menuIds.append(jsonMenuIds.at(i).toInt());
    }
    role.menuIds = menuIds;

    QJsonArray jsonDeptIds = jsonRole.value("deptIds").toArray();
    QList<int> deptIds;
    for(int i = 0;i< jsonDeptIds.size();i++){
        deptIds.append(jsonDeptIds.at(i).toInt());
    }
    role.deptIds = deptIds;

    QJsonArray jsonPermissions = jsonRole.value("permissions").toArray();
    QStringList permissions;
    for(int i = 0;i< jsonPermissions.size();i++){
        permissions.append(jsonPermissions.at(i).toString());
    }
    role.permissions = permissions;
    role.admin = jsonRole.value("admin").toInt();
    return role;
}

Post Utils::getPost(QJsonObject jsonPost)
{
    Post post;
    post.createBy = jsonPost.value("createBy").toString();
    post.createTime = jsonPost.value("createTime").toString();
    post.flag = jsonPost.value("flag").toBool();
    post.postCode = jsonPost.value("postCode").toString();
    post.postId = jsonPost.value("postId").toInt();
    post.postName = jsonPost.value("postName").toString();
    post.postSort = jsonPost.value("postSort").toInt();
    post.remark = jsonPost.value("remark").toString();
    post.status = jsonPost.value("status").toString();
    post.updateBy = jsonPost.value("updateBy").toString();
    post.updateTime = jsonPost.value("updateTime").toString();
    return post;
}

void Utils::load()
{
    QDialog *dlg = new QDialog(0);
    Qt::WindowFlags flags=Qt::Dialog;
    flags |=Qt::WindowCloseButtonHint;
    dlg->setWindowFlags(flags);
    dlg->setWindowIcon(QIcon(":/image/res/6.ico"));
    dlg->setFixedSize(150,1);
    dlg->show();
    dlg->setWindowTitle("请稍后...");
    QThread::msleep(1000);
    dlg->close();
}

void Utils::settableWidget_userManager(QList<User> *userList, QTableWidget *tableWidget,QStringList tableColimnList)
{
    tableWidget->clear();
    tableWidget->setColumnCount(tableColimnList.size());
    tableWidget->setHorizontalHeaderLabels(tableColimnList);
    tableWidget->setRowCount(userList->size());
    tableWidget->setColumnWidth(7,190);
    for(int i = 0;i< userList->size();i++){
        tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(userList->at(i).userId)));
        tableWidget->setItem(i,1,new QTableWidgetItem(userList->at(i).userName));
        tableWidget->setItem(i,2,new QTableWidgetItem(userList->at(i).nickName));
        tableWidget->setItem(i,3,new QTableWidgetItem(userList->at(i).dept.deptName));
        tableWidget->setItem(i,4,new QTableWidgetItem(userList->at(i).phonenumber));
        QCheckBox *box = new QCheckBox();
        box->setCheckState(userList->at(i).status == "0" ? Qt::Checked : Qt::Unchecked);
        connect(box,&QCheckBox::clicked,this,[=](){
            QString info = QString("确定要【%1】【%2】用户吗?").arg(userList->at(i).status == "0" ? "停用" : "启用",userList->at(i).userName);
            int flag = QMessageBox::question(0,"系统提示",info,QMessageBox::No | QMessageBox::Yes);
            if(flag == QMessageBox::Yes){
                QString status = userList->at(i).status == "0" ? "1" : "0";
                QString baseUrl =Config::baseUrl;
                QNetworkRequest request;
                QNetworkAccessManager manager;
                request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));//post请求设置为json，才能发送josn格式文件
                request.setRawHeader("Authorization",  qApp->property("token").value<QString>().toLocal8Bit());
                request.setUrl(QUrl(baseUrl + "system/user/changeStatus"));
                //创建一个Json对象,这个Json对象就是要发送的内容，展开成Json数据如上所示
                QJsonObject jsonObj;
                jsonObj.insert("userId",userList->at(i).userId);
                jsonObj.insert("status",status);

                QJsonDocument jsonDoc = QJsonDocument(jsonObj);
                QByteArray postData = jsonDoc.toJson(QJsonDocument::Compact);
                QNetworkReply* reply = manager.put(request,postData);
                //同步请求
                QEventLoop eventLoop;
                connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
                eventLoop.exec();
                QByteArray bytes = reply->readAll().data();
                QJsonObject obj = QJsonDocument::fromJson(bytes).object();
                QMessageBox::information(0,"提示",obj.value("msg").toString());
                if(obj.value("code").toInt() != 200){
                    box->setChecked(!box->isChecked());
                    return;
                }
                emit stateChangeSuccess();
            }else{
                box->setChecked(!box->isChecked());
            }
        });
        QWidget *widget = new QWidget();
        QHBoxLayout *layout = new QHBoxLayout();
        layout->addWidget(box);
        layout->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        widget->setLayout(layout);
        tableWidget->setCellWidget(i,5,widget);
        tableWidget->setItem(i,6,new QTableWidgetItem(userList->at(i).createTime));
        tableWidget->item(i,0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        tableWidget->item(i,1)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        tableWidget->item(i,2)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        tableWidget->item(i,3)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        tableWidget->item(i,4)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        tableWidget->item(i,6)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    }
}

void Utils::filterTreeWidget(QTreeWidgetItem *item, QString text)
{
    int childCount = item->childCount();
    bool flag = 0;
    for(int i = 0;i< childCount;i++){
        QTreeWidgetItem *childItem = item->child(i);
        filterTreeWidget(childItem,text);
        if(!childItem->isHidden()){
            flag = 1;
        }
    }
    if(item->text(0).contains(text,Qt::CaseInsensitive) || flag){
        item->setHidden(0);
    }else{
        item->setHidden(1);
    }
}

//第一个参数是页面上的表格，第二个是导出文件的表头数据
void Utils::exportExcel(QTableWidget *table, QString title){
    //进度条
    QProgressDialog d;
    d.setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint);		//不显示进度条上的“最小化”“最大化”“关闭”
    d.setWindowTitle("任务保存");	//窗口标题
    d.setAutoClose(false);//进度达到最大值时不关闭，默认为true
    d.setLabelText("任务保存中...");	//显示的文本
    d.setCancelButton(NULL);			//不显示取消按钮
    d.setRange(0,100);
    d.setValue(0);
    d.show();
    QString fileName = QFileDialog::getSaveFileName(table, "保存",QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation),"Excel 文件(*.xls *.xlsx)");
    if (fileName!=""){
        QAxObject *excel = new QAxObject;
        d.raise();// 将窗口置于最前面
        if (excel->setControl("Excel.Application")){ //连接Excel控件{
            excel->dynamicCall("SetVisible (bool Visible)","false");//不显示窗体
            excel->setProperty("DisplayAlerts", false);//不显示任何警告信息。如果为true那么在关闭是会出现类似“文件已修改，是否保存”的提示
            QAxObject *workbooks = excel->querySubObject("WorkBooks");//获取工作簿集合
            d.setValue(5);
            workbooks->dynamicCall("Add");//新建一个工作簿
            QAxObject *workbook = excel->querySubObject("ActiveWorkBook");//获取当前工作簿
            d.setValue(10);
            QAxObject *worksheet = workbook->querySubObject("Worksheets(int)", 1);
            d.setValue(15);

            int i,j;
            //QTablewidget 获取数据的列数
            int colcount=table->columnCount();
            //QTablewidget 获取数据的行数
            int rowcount=table->rowCount();
            //每条数据的百分比
            int per = 70 / rowcount;
            //如果条目太多，per=0，进度条不会动，
            if(per == 0) per = 1;

            QAxObject *cell,*col;
            //标题行
            cell=worksheet->querySubObject("Cells(int,int)", 1, 1);
            cell->dynamicCall("SetValue(const QString&)", title);
            cell->querySubObject("Font")->setProperty("Size", 18);
            //调整行高
            worksheet->querySubObject("Range(const QString&)", "1:1")->setProperty("RowHeight", 30);
            //合并标题行
            QString cellTitle;
            cellTitle.append("A1:");
            cellTitle.append(QChar(colcount - 1 + 'A'));
            cellTitle.append(QString::number(1));
            QAxObject *range = worksheet->querySubObject("Range(const QString&)", cellTitle);
            range->setProperty("WrapText", true);
            range->setProperty("MergeCells", true);
            range->setProperty("HorizontalAlignment", -4108);//xlCenter
            range->setProperty("VerticalAlignment", -4108);//xlCenter

            d.setValue(20);

            //列标题
            for(i=0;i<colcount;i++){
                QString columnName;
                columnName.append(QChar(i  + 'A'));
                columnName.append(":");
                columnName.append(QChar(i + 'A'));
                col = worksheet->querySubObject("Columns(const QString&)", columnName);
                col->setProperty("ColumnWidth", table->columnWidth(i)/6);
                cell=worksheet->querySubObject("Cells(int,int)", 2, i+1);
                //QTableWidget 获取表格头部文字信息
                columnName=table->horizontalHeaderItem(i)->text();
                //QTableView 获取表格头部文字信息
                // columnName=ui->tableView_right->model()->headerData(i,Qt::Horizontal,Qt::DisplayRole).toString();
                cell->dynamicCall("SetValue(const QString&)", columnName);
                cell->querySubObject("Font")->setProperty("Bold", true);
                cell->querySubObject("Interior")->setProperty("Color",QColor(191, 191, 191));
                cell->setProperty("HorizontalAlignment", -4108);//xlCenter
                cell->setProperty("VerticalAlignment", -4108);//xlCenter
            }

            d.setValue(25);

            //数据区
            //QTableWidget 获取表格数据部分
            for(i=0;i<rowcount;i++){
                d.setValue(d.value() + i * per);
                for (j=0;j<colcount;j++){
//                    if(qobject_cast<QLabel*>(table->cellWidget(i, j)) != nullptr){
//                        QLabel* label = qobject_cast<QLabel*>(table->cellWidget(i, j));
//                        worksheet->querySubObject("Cells(int,int)", i+3, j+1)->dynamicCall("SetValue(const QString&)", label->text());
//                    }else if(qobject_cast<QCheckBox*>(table->cellWidget(i, j))){
//                        QCheckBox* box = qobject_cast<QCheckBox*>(table->cellWidget(i, j));
//                        worksheet->querySubObject("Cells(int,int)", i+3, j+1)->dynamicCall("SetValue(const QString&)", box->isChecked() ? "正常" : "停用");
//                    }else{
//                        worksheet->querySubObject("Cells(int,int)", i+3, j+1)->dynamicCall("SetValue(const QString&)", table->item(i,j)?table->item(i,j)->text():"");
//                    }
                    worksheet->querySubObject("Cells(int,int)", i+3, j+1)->dynamicCall("SetValue(const QString&)", table->item(i,j)?table->item(i,j)->text():"");

                }
            }

            d.setValue(95);

            //画框线
            QString lrange;
            lrange.append("A2:");
            lrange.append(QString::number(colcount - 1) + 'A');
            lrange.append(QString::number(table->rowCount() + 2));
            range = worksheet->querySubObject("Range(const QString&)", lrange);
            range->querySubObject("Borders")->setProperty("LineStyle", QString::number(1));
            range->querySubObject("Borders")->setProperty("Color", QColor(0, 0, 0));
            //调整数据区行高
            QString rowsName;
            rowsName.append("2:");
            rowsName.append(QString::number(table->rowCount() + 2));
            range = worksheet->querySubObject("Range(const QString&)", rowsName);
            range->setProperty("RowHeight", 20);
            workbook->dynamicCall("SaveAs(const QString&)",QDir::toNativeSeparators(fileName));//保存至fileName
            workbook->dynamicCall("Close()");//关闭工作簿
            excel->dynamicCall("Quit()");//关闭excel
            delete excel;
            excel=NULL;
            d.setValue(100);
            if (QMessageBox::question(NULL,"完成","文件已经导出，是否现在打开？",QMessageBox::Yes|QMessageBox::No)==QMessageBox::Yes){
                QDesktopServices::openUrl(QUrl("file:///" + QDir::toNativeSeparators(fileName)));
            }
        }else{
            QMessageBox::warning(NULL,"错误","未能创建 Excel 对象，请安装 Microsoft Excel。",QMessageBox::Apply);
        }
    }else{
         QMessageBox::warning(NULL,"提示","任务取消",QMessageBox::Apply);
    }
}

QNetworkReply *Utils::uploadFile(QString filePath, QString url)
{
    //构造manager
    QNetworkAccessManager *manager = new QNetworkAccessManager();

    //构造multiPart ，用来组装文件数据
    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    QHttpPart imagePart;
    //经测试，设置此content-type，并不是必须。我测试的为jpg文件，则是image/jpeg，其他的格式文件查找http状态码，或者查找HTTP  content-type
    imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("multipart/form-data"));

    QString filename = filePath.mid(filePath.lastIndexOf("/") + 1);
    //此ContentDispositionHeader参数极为重要，如果不对，则发送不成功，name可以随意起名字，但是filename则必须是你要上传的文件名。  name就是服务器接收的参数名
    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QString("form-data; name=\"%1\"; filename=\"%2\"").arg("file").arg(filename));

    QFile *file = new QFile(filePath);
    file->open(QIODevice::ReadOnly);
    //依照下面方式将文件的内容附着于multiPart上，
    imagePart.setBodyDevice(file);
    file->setParent(multiPart);
    multiPart->append(imagePart);

    //此处设置上传的url
    QNetworkRequest request(url);
    request.setRawHeader("Authorization",  qApp->property("token").value<QString>().toLocal8Bit());
    //发送post
    QNetworkReply *reply = manager->post(request, multiPart);

    QEventLoop eventLoop;
    connect(manager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
    eventLoop.exec();
    file->close();
    return reply;
}

void Utils::requestCheck(QByteArray *data)
{
    if(data->isEmpty()){
        QMessageBox::warning(0,"提示","服务器连接失败");
        return;
    }
    QJsonObject obj = QJsonDocument::fromJson(*data).object();
    if(200 == obj.value("code").toInt()){
        return;
    }else if(401 == obj.value("code").toInt()){
        int flag = QMessageBox::question(0,"提示",obj.value("msg").toString(),QMessageBox::Yes | QMessageBox::No);
//        if(flag == QMessageBox::Yes){
//            emit back401();
//            qDebug()<<3;
//        }
        return;
    }else{
        QMessageBox::warning(0,"提示",obj.value("msg").toString());
        return;
    }
}

void Utils::updateParentItem(QTreeWidgetItem *item)
{
    QTreeWidgetItem *parent = item->parent();
    if(parent == NULL){
        return;
    }
    int selectCount = 0;
    int childCount = parent->childCount();
    for(int i = 0;i< childCount;i++){
        QTreeWidgetItem *childrenItem = parent->child(i);
        if(childrenItem->checkState(0) == Qt::Checked){
            selectCount++;
        }
    }
    if(selectCount <= 0){
        parent->setCheckState(0,Qt::Unchecked);
    }else if(selectCount > 0 && selectCount < childCount){
        parent->setCheckState(0,Qt::PartiallyChecked);
    }else if(selectCount == childCount){
        parent->setCheckState(0,Qt::Checked);
    }
}

void Utils::selectAll(QTreeWidgetItem *item)
{
    item->setCheckState(0,Qt::Checked);
    for(int i = 0;i< item->childCount();i++){
        selectAll(item->child(i));
    }
}

void Utils::unSelectAll(QTreeWidgetItem *item)
{
    item->setCheckState(0,Qt::Unchecked);
    for(int i = 0;i< item->childCount();i++){
        unSelectAll(item->child(i));
    }
}

void Utils::getMenuIds(QTreeWidgetItem *item,QList<int> *menuIds)
{
    int count = item->childCount();
    if(count <= 0) return;
    for(int i = 0;i< count;i++){
        QTreeWidgetItem * childItem = item->child(i);
        if(childItem->checkState(0) == Qt::Checked){
            menuIds->append(childItem->data(1,0).toInt());
        }
        getMenuIds(childItem,menuIds);
    }
}

void Utils::setOwnMenu(int id, QTreeWidgetItem *item)
{
    if(id == item->data(1,0)){
        item->setCheckState(0,Qt::Checked);
        selectAllSonNode(item);
    }else{
        for(int i = 0;i< item->childCount();i++){
            QTreeWidgetItem *childItem = item->child(i);
            setOwnMenu(id,childItem);
        }
    }

}

void Utils::selectAllSonNode(QTreeWidgetItem *item){
    for(int i = 0;i< item->childCount();i++){
        QTreeWidgetItem *childItem = item->child(i);
        childItem->setCheckState(0,Qt::Checked);
        selectAllSonNode(childItem);
    }
}

QByteArray * Utils::getDictType(QString type)
{
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));//post请求设置为json，才能发送josn格式文件
    request.setRawHeader("Authorization",  qApp->property("token").value<QString>().toLocal8Bit());
    QString baseUrl = Config::baseUrl;
    QNetworkAccessManager *manager = new QNetworkAccessManager(0);
    QByteArray *data = nullptr;
    request.setUrl(baseUrl + "system/dict/data/type/" + type);
    QNetworkReply *reply= manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    requestCheck(data);
    delete manager;
    return data;
}







